﻿using Business;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAppWeb.Controllers
{
    public class AreaPortController : BaseDataController
    {
        //
        // GET: /AreaPort/

        public ActionResult AreaPort()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);
            }
        }

        public JsonResult Query(string Area, string SendPort)
        {
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            int n = 0;
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(Area))
            {
                if (int.TryParse(Area, out n))
                {
                    if (Convert.ToInt32(Area) != 0)
                    {
                        where += " and t1.areaid='" + Area + "'";
                    }
                }
                else
                {
                    where += " and 1=2";
                }
            }

            if (!string.IsNullOrEmpty(SendPort))
            {
                if (int.TryParse(SendPort, out n))
                {
                    if (Convert.ToInt32(SendPort) != 0)
                    {
                        where += " and t1.sendportid='" + SendPort + "'";
                    }
                }
                else
                {
                    where += " and 1=2";
                }
            }

            BAreaSendPort nc = new BAreaSendPort();
            var result = nc.GetAreaSendPortList(page, rows, out total, where);


            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", result);
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        public JsonResult AreaList(int IsSetAll)
        {
            var retData = new object();
            BAreaSendPort nbl = new BAreaSendPort();
            retData = nbl.GetAreaList(IsSetAll).ToList();
            return Json(retData);
        }
        public JsonResult SendPortList(int IsSetAll)
        {
            var retData = new object();
            BAreaSendPort nbl = new BAreaSendPort();
            retData = nbl.GetSendPortList(IsSetAll).ToList();
            return Json(retData);
        }

        private string CheckAreaPort(string AreaPortId,string Areaid,string SendPortId,string Remark, out AreaPort areaport)
        {
            string sjson = string.Empty;
            AppUserList model = (AppUserList)Session["UserInfo"];
            areaport = new Model.AreaPort();
            areaport.AREAPORTID = string.IsNullOrEmpty(AreaPortId) ? 0 : int.Parse(AreaPortId);
            areaport.AREAID = string.IsNullOrEmpty(Areaid) ? 0 : int.Parse(Areaid);
            areaport.SENDPORTID = string.IsNullOrEmpty(SendPortId) ? 0 : int.Parse(SendPortId);
            areaport.REMARK = Remark.Trim();

            BAreaSendPort nbl = new BAreaSendPort();
            AreaPort checkareaport=new AreaPort ();
            if (areaport.AREAPORTID == 0)//新增
            {
                checkareaport = nbl.GetAreaPortByAreaID(Areaid);
            }
            else//修改
            {
                checkareaport = nbl.GetAreaPortByAreaIDWithoutID(Areaid, AreaPortId);
            }
            if (checkareaport != null)
            {
                sjson = "{success:false,msg:'该区域对应发货口关系已存在！'}";
                return sjson;
            }
            return sjson;
        }

        [HttpPost]
        public string Saves(string AreaPortID)
        {
            string sjson = string.Empty;
            try
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                string strAreaPortID = Request["hiddenAREAPORTID"];
                string strAreaID = Request["AreaID1"];

                string strSendPortID = Request["PortID1"];
                string strRemark = Request["txtRemark"];
                AreaPort areaport = new AreaPort();

                sjson = CheckAreaPort(strAreaPortID, strAreaID, strSendPortID,strRemark, out areaport);

                if (!string.IsNullOrEmpty(sjson))
                    return sjson;

                BAreaSendPort nbl = new BAreaSendPort();
                if (string.IsNullOrEmpty(strAreaPortID))
                {
                    areaport.CREATER = model.USERNAME;
                    areaport.CREATETIME = System.DateTime.Now;
                    nbl.AddAreaSendPort(areaport);
                }
                else
                {
                    areaport.MODITOR = model.USERNAME;
                    areaport.MODITIME = System.DateTime.Now;
                    areaport.AREAPORTID = int.Parse(strAreaPortID);
                    nbl.EditAreaSendPort(areaport);
                }
                sjson = "{success:true}";
            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;
        }

        [HttpPost]
        public string Delete(string ids)
        {
            string sjson = string.Empty;
            try
            {
                BAreaSendPort nbl = new BAreaSendPort();
                foreach (var id in ids.Split(','))
                {
                    nbl.DeleteAreaSendPort(int.Parse(id));
                }
                sjson = "{success:true}";
            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;

        }
    }
}
